<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /**
     * https://leetcode-cn.com/problems/compare-version-numbers/solution/si-lu-qing-xi-javascriptfen-ge-zi-fu-chuan-xun-hua/
     * 思路：将字符串转换为字符串数组，比较每个元素(转换为整数后)的大小
            ~~a 可以把数字类型的字符串转换成数字类型，如果是undefined，会转换成0
     */ 
    let version1 = '1.4.2';
    let version2 = '1.2.3';

    function compareVersion(version1, version2) {
      let v1 = version1.split('.');
      let v2 = version2.split('.');

      let maxLength = Math.max(v1.length, v2.length);

      for (let i = 0; i < maxLength; i++) {
        if (~~v1[i] > ~~v2[i])
          return 1;
        else if (~~ v1[i] < ~~v2[i])
          return -1;
      }
      return 0;
    }
    console.log(compareVersion(version1, version2));
  </script>
</body>
</html>